i=5
print i
p = (4, 5)
x, y = p
print x
data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]
name, shares, price, date = data
print shares
ata = [ 'ACME', 50, 91.1, (2012, 12, 21) ]
o,name,o,date=ata
print date
record = ('Dave', 'dave@example.com')  
name, email = record
print name

#1.3
from collections import deque

q = deque(maxlen=3)
q.append(1)
q.append(2)
q.append(3)
print q
deque([1, 2, 3], maxlen=3)
q.append(4)
print q
deque([2, 3, 4], maxlen=3)
q.append(5)
print q
deque([3, 4, 5], maxlen=3)

q = deque()
q.append(1)
q.append(2)
q.append(3)
print q
deque([1, 2, 3])
q.appendleft(4)
print q
deque([4, 1, 2, 3])
q.pop()
print q
deque([4, 1, 2])
q.popleft()
print q

#1.4
import heapq

nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]
print heapq.heappop(nums)
print nums
print heapq.heappop(nums)
print nums
print heapq.heappop(nums)
print nums
print heapq.heappop(nums)
print nums
print heapq.heappop(nums)
print heapq.heappop(nums)
print nums


portfolio = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'name': 'FB', 'shares': 200, 'price': 21.09},
    {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
print cheap
print expensive


#1.5
import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0

    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1

    def pop(self):
        return heapq.heappop(self._queue)[-1]
class Item:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return 'Item({!r})'.format(self.name)
q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1)
q.pop()
Item('bar')
print q.pop()
Item('spam')
print q.pop()
Item('foo')
print q.pop()
Item('grok')



#1.6
from collections import defaultdict

d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(4)

print d['a']
print d['b']

d = defaultdict(set)
d['a'].add(1)
d['a'].add(2)
d['b'].add(4)

print d['a']
print d['b']


#1.7
from collections import OrderedDict

d = OrderedDict()
d['foo'] = 1
d['bar'] = 2
d['spam'] = 3
d['grok'] = 4
print d
for key in d:
    print(key, d[key])


#1.8
prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
min_price = min(zip(prices.values(), prices.keys()))
print min_price
max_price = max(zip(prices.values(), prices.keys()))
print max_price
prices_sorted = sorted(zip(prices.values(), prices.keys()))
print prices_sorted                #sorted dic paixu

print min(prices)
print min(prices.values())


#1.9
a = {
    'x' : 1,
    'y' : 2,
    'z' : 3
}

b = {
    'w' : 10,
    'x' : 11,
    'y' : 2
}
print a.keys()
#can't  a.keys() & b.keys()


1.10
#utf-8
#is hashable
def dedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
        seen.add(item)
a = [1, 5, 2, 1, 9, 1, 5, 10]
print list(dedupe(a))

#is not hashable
def dedupe(items, key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)
a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]
print list(dedupe(a, key=lambda d: (d['x'],d['y'])))
print list(dedupe(a, key=lambda d: d['x']))


1.11
record = '....................100 .......513.25 ..........'
cost = int(record[20:23]) * float(record[31:37])
print cost
SHARES = slice(20, 23)
PRICE = slice(31, 37)
cost = int(record[SHARES]) * float(record[PRICE])
print record[SHARES]
print record[PRICE]

items = [0, 1, 2, 7, 4, 5, 6]
print items[2:7]  #canshu 0+to1+


1.12
words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
    'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
    'my', 'eyes', "you're", 'under'
]
from collections import Counter
word_counts = Counter(words)     
top_three = word_counts.most_common(3)
print(top_three)
print word_counts['eyes']


1.13
rows = [
    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]
from operator import itemgetter
rows_by_fname = sorted(rows, key=itemgetter('fname'))
rows_by_uid = sorted(rows, key=itemgetter('uid'))
print(rows_by_fname)
print(rows_by_uid)

print "hello world"


#1.14
class User:
    def __init__(self, user_id):
        self.user_id = user_id

    def __repr__(self):
        return 'User({})'.format(self.user_id)


def sort_notcompare():
    users = [User(23), User(3), User(99)]
    print(users)
    print(sorted(users, key=lambda u: u.user_id))
	

#1.15
print 1.15
rows = [
    {'address': '5412 N CLARK', 'date': '07/01/2012'},
    {'address': '5148 N CLARK', 'date': '07/04/2012'},
    {'address': '5800 E 58TH', 'date': '07/02/2012'},
    {'address': '2122 N CLARK', 'date': '07/03/2012'},
    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
    {'address': '1060 W ADDISON', 'date': '07/02/2012'},
    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},
    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]
from operator import itemgetter
from itertools import groupby

# Sort by the desired field first
rows.sort(key=itemgetter('address'))
# Iterate in groups
for date, items in groupby(rows, key=itemgetter('address')):
    
    for i in items:
        print(' ', i)
		
		
		
#2.2.1.2
print ord('a')
print chr(98)


'''
nodes=xpath("li/a")
for n in nodes:
    print n.text
'''

#2.2.1.3
def isAString(anobj):
	return isinstance(anobj,basestring)
	
#2.2.1.4
x = "    hehe   "
print '|', x.lstrip( ), '|', x.rstrip( ), '|' ,x.strip( ),'|'

#2.2.1.5
'''
pieces = ('1','2','3','4')
largeString = ' '.join(pieces)
	largeString = '%s%s something %s yet more' %s(small,small)
print largeString
'''


#2.2.1.6
revwords = 'asd sad sd asd'
'''
revwords = astring.split( )
revwords.reverse( )
revwords = ' '.join(revwords)
print revwords

revwords = ' '.join(astring.split( )[::-1])
print revwords
'''


'''
xpath could copy text
how to make python link to html

eg = etree.HTML(open('new 1.html').read()).xpath('//ul/li')
print eg.xpath('text')

'''












